package cn.fayang.org.controller;

import cn.fayang.basic.util.AjaxResult;
import cn.fayang.basic.util.PageList;
import cn.fayang.org.domain.Department;
import cn.fayang.org.query.DepartmentQuery;
import cn.fayang.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 控制器
 *
 */
@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 通过ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);

    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    public List<Department> qureyAll(){
        return departmentService.queryAll();
    }

    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    @PostMapping
    public PageList<Department> querPage(@RequestBody DepartmentQuery departmentQuery){
        return  departmentService.queryPage(departmentQuery);
    }

    /**
     * 删除
     * @param id
     * @return
     */

    @DeleteMapping("{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.deleteByID(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后再试");
        }
    }
    @PatchMapping
    public AjaxResult Batchremove(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后再试");
        }
    }

    /**
     * 增加和修改
     * @param department
     * @return
     */
    @PutMapping
    public  AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后再试");
        }
    }
    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.querTree();

    }



}
